using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace LogAnalyzerIIS.Helpers
{
	public class EncryptionHelper
	{
		private SymmetricAlgorithm mCSP;

		public EncryptionHelper()
		{
			mCSP = new TripleDESCryptoServiceProvider();
			mCSP.IV = Convert.FromBase64String("vi11MwPc4wY=");
			mCSP.Key = Convert.FromBase64String("ZVGRvz1n2DRERI85rTRxj/iifQKaE6ZP");
		}

		public string EncryptString(string inputString)
		{
			byte[] stringBytes = Encoding.UTF8.GetBytes(inputString);

			ICryptoTransform transform = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV);
			MemoryStream memoryStream = new MemoryStream();

			CryptoStream cryptoStream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Write);
			cryptoStream.Write(stringBytes, 0, stringBytes.Length);
			cryptoStream.FlushFinalBlock();
			cryptoStream.Close();

			return Convert.ToBase64String(memoryStream.ToArray());
		}

		public string DecryptString(string inputString)
		{
			byte[] stringBytes = Convert.FromBase64String(inputString);

			ICryptoTransform transform = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV);
			MemoryStream memoryStream = new MemoryStream();

			CryptoStream cryptoStream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Write);
			cryptoStream.Write(stringBytes, 0, stringBytes.Length);
			cryptoStream.FlushFinalBlock();
			cryptoStream.Close();

			return Encoding.UTF8.GetString(memoryStream.ToArray());
		}
	}
}
